package hadoop.mr02;


import hadoop.unit.GlobalConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;


public class Partition_driver extends Configured implements Tool {
    private static Configuration conf = GlobalConfiguration.getCon();

    @Override
    public int run(String[] args) throws Exception {
        // 判断是否有输入路径和输出路径
        if (args.length < 2) {
            System.err.println("NO TASK");
            System.exit(2); // 输出2为异常退出
        }
        Job job = Job.getInstance(conf, "foodKinds");

        job.setJarByClass(Partition_driver.class);
        job.setMapperClass(Partition_Mapper.class);
        job.setReducerClass(Partition_Reducer.class);

        // 导入自定义分区类并制定分区个数，且分区个数与自定义个数一致
        job.setPartitionerClass(student.class);
        job.setNumReduceTasks(3);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        Path in_path = new Path(args[0]);
        FileInputFormat.setInputPaths(job, in_path);
        Path out_path = new Path(args[1]);
        FileOutputFormat.setOutputPath(job, out_path);

        FileSystem fs = FileSystem.get(conf);
        if(fs.exists(out_path)){
            fs.delete(out_path,true);
        }
        return job.waitForCompletion(true)?0:2;
    }
    public static void main(String[] args) throws Exception {
        int run = ToolRunner.run(conf, new Partition_driver(), args);
        System.exit(run);
    }
}

